import request from "./request";

export function login(data) {
  return request({
    method: "POST",
    url: "/login/",
    data: data,
  });
}

/*
获取用户信息，id为用户号，0为获取当前用户自己的信息
*/
export function getUser(id) {
  return request({
    method: "GET",
    url: "/user/" + id + "/",
  });
}

export function updateUser(id, data) {
  return request({
    method: "PUT",
    url: "/user/" + id + "/",
    data: data,
  });
}

export function resetPassword(id) {
  return request({
    method: "PUT",
    url: "/user/" + id + "/",
    data: {
      password: "123456",
    },
  });
}

export function getSchoolAdminDetail(id) {
  return request({
    method: "GET",
    url: "/user/school_admin/" + id + "/",
  });
}

export function getStudentList() {
  return request({
    method: "GET",
    url: "/user/student/",
  });
}

export function getTeacherList() {
  return request({
    method: "GET",
    url: "/user/teacher/",
  });
}
// 学校管理员
export function getSchooladminList() {
  return request({
    method: "GET",
    url: "/user/school_admin/",
  });
}
// 修改学校管理员的激活状态is_active
export function updateUserActiveState(ID, Isactive) {
  return request({
    method: "PUT",
    url: "user/" + ID + "/",
    data: {
      is_active: Isactive,
    },
  });
}
// 学校管理员添加
export function createSchoolAdmin(realname, id, phone, email) {
  return request({
    method: "POST",
    url: "user/school_admin/",
    data: {
      real_name: realname,
      school: id,
      phone: phone,
      email: email,
    },
  });
}
export function adminDelect(adminid) {
  return request({
    method: "DELETE",
    url: "user/school_admin/" + adminid + "/",
  });
}

// 管理员用户修改
// Bug:调用用户API不能改学校，调用管理员修改APL只能改学校
export function AdminModify(schoolid, adminid) {
  return request({
    method: "PUT",
    url: "user/school_admin/" + adminid + "/",
    data: {
      school: schoolid,
    },
  });
}

async function downloadTemplate(id) {
  // id = 1 下载学生模板
  // id = 2, 下载老师模板
  let response = await request({
    method: "GET",
    url: "/user/file/template/" + id + "/",
    responseType: "blob",
  });

  const blob = new Blob([response], {
    type: "applicationnd.openxmlformats-officedocument.spreadsheetml.sheet",
  });

  // 创建一个 URL 对象，并生成下载链接
  const url = URL.createObjectURL(blob);
  const link = document.createElement("a");
  link.href = url;
  if (id === 1) {
    link.download = "学生账号创建模板.xlsx";
  } else {
    link.download = "老师账号创建模板.xlsx";
  }

  // 模拟点击下载链接
  link.click();

  // 释放 URL 对象
  URL.revokeObjectURL(url);
}

// 老师批量添加函数
export function teacherbatchadd(data) {
  //FormData 是一个 JavaScript 对象，用于构建一组键值对，
  // 通常与表单提交相关。在这个例子中，创建了一个名为 formdata 的新 FormData 对象。
  let formdata = new FormData();
  formdata.append("file", data);
  return request({
    method: "POST",
    url: "/user/teacher/createfromexcel/",
    data: formdata,
    file: "xlsx", //格式
  });
}

//学生模板下载
export function DownloadStudentTemplate() {
  downloadTemplate(1);
}
//老师模板下载
export function DownloadTeacherTemplate() {
  downloadTemplate(2);
  // window.location.href = "api/user/file/template/1/";
}

//老师工号修改
export function TeacherModify(id, faculty_number, academy) {
  return request({
    method: "PUT",
    url: "user/teacher/" + id + "/",
    data: { faculty_number: faculty_number, academy: academy },
  });
}

export function ModifyTeacherUser(id, data) {
  return request({
    method: "PUT",
    url: "user/" + id + "/",
    data: data,
  });
}

//老师状态切换
export function TeacherStateModify(id, bool) {
  return request({
    method: "PUT",
    url: "user/" + id + "/",
    data: { is_active: bool },
  });
}

//老师添加
export function TeacherAdd(data) {
  return request({
    method: "POST",
    url: "user/teacher/",
    data: data,
  });
}

//老师删除
export function TeacherDelete(id) {
  return request({ method: "DELETE", url: "user/teacher/" + id + "/" });
}

//导师分配学生
export function SelectStudentTeacher(studentid, teacherid) {
  return request({
    method: "PUT",
    url: "user/student/" + studentid + "/upt_foreign_pro/",
    data: {
      supervisor: teacherid,
    },
  });
}

//管理员修改自己信息
export function updataSecurityInfo(data) {
  return request({
    method: "put",
    url: "user/security/modify/",
    data: data,
  });
}
